package com.project.collectAPI.controller;

import com.project.collectAPI.comm.*;
import com.project.collectAPI.service.*;
import com.project.collectAPI.comm.RedisUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 会员信息表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-11-17 11:25:35
 */

/*需要注意：必须使用@Controller，不能使用@RestController。
因为@RestController返回的是  JSON，且不走SpringMVC的视图解析流程，所以跳不到html那里。*/
@Controller
@Slf4j
@RequestMapping("/")
public class IndexController {

    @Autowired
    private RedisUtils redisUtils;

    @Resource
    private FirstclassService firstclassService;

    @Resource
    private SecondclassService secondclassService;

    @Resource
    private SavedataService savedataService;


    //ajax主机IP地址
    public static final String ServerIP= Constant.ServerIP;



    /**
     * 店铺信息
     * */
/*    @PostMapping(value = {"/storeInfo"})
    //ISO-8859-1 GBK GB2312 UTF-8
    //@RequestMapping(value = "/storeInfo", produces = "application/json;charset=UTF-8")
    public String storeInfo(Model model, HttpServletRequest request) throws IOException {

        model.addAttribute("ServerIP",ServerIP);
        model.addAttribute("pageName","店铺信息");

        *//*System.out.println("菜单事件地址url######################################："+request.getParameter("url"));
        System.out.println("菜单事件资源result$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$："+result);
        System.out.println("菜单事件from数据*************************************："+request.getParameter("form"));*//*
        System.out.println("菜单事件headers**************************************："+request.getParameter("headers"));

        //捕获头部信息并保存
        doWork.headers=doWork.catchHeaders(request.getParameter("headers"));
        //捕获店铺信息并保存
        String result=request.getParameter("result");

        //获取店铺信息
        if(!StringUtils.isEmpty(result)) {
            doWork.getStoreItemInfo(result);
        }
        return "index";
    }*/




    /**
     * 商品信息
     * */
    @PostMapping(value = {"/productInfo"})
    public String productInfo(Model model, HttpServletRequest request) throws Exception {
        //捕获店铺信息并保存
        String result=request.getParameter("result");
        /*System.out.println("沃尔玛商品信息================>");
        System.out.println(result);*/
        //获取商品信息
        if(!StringUtils.isEmpty(result)) {
            new Thread(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    System.out.println("启动一个线程...getProductInfoForMultiStore");
                    //doWork.getProductInfo(result);  //单店铺
                    doWork.getProductInfoForMultiStore(result); //多店铺
                }
            }).start();
        }
        return "index";
    }



    /**
     * 获取菜单信息
     * */
    @PostMapping(value = {"/ItemInfo"})
    public String ItemInfo(Model model, HttpServletRequest request) throws Exception {
        //捕获店铺信息并保存
        String result=request.getParameter("result");
        System.out.println("菜单信息================>");
        System.out.println(result);
        //获取商品信息
        if(!StringUtils.isEmpty(result)) {
           doWork.getItem(result);
        }
        return "index";
    }



    /*
     * 获取所有类和id,需要获取才打开
     * */
    @PostMapping(value = {"/getAllClassTitleAndId"})
    public String getAllClassTitleAndId(Model model, HttpServletRequest request) throws Exception {
        /*String result=request.getParameter("result");
        System.out.println("获取所有类和id ================>");
        System.out.println(result);
        //获取商品信息
        if(!StringUtils.isEmpty(result)) {
            doWork.getAllClassTitleAndIdToMap(result);
        }*/
        return "index";
    }


 } //Class Controller End




